package com.amap.demo.view;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 地理编码与反地理编码
 * 1. 地理编码
 * 2. 反地理编码
 *
 * @author ys
 */
public class GeoOrResGeoActivity extends ParentActivity implements GeocodeSearch.OnGeocodeSearchListener {

    @Bind(R.id.geo_map)
    MapView mapView;
    @Bind(R.id.geo_iv)
    ImageView geoIv;
    @Bind(R.id.res_geo_btn)
    Button resGeoBtn;

    /**
     *
     */
    private AMap aMap;

    private LatLng latLng;

    private GeocodeSearch geocodeSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geo_or_res_geo);
        ButterKnife.bind(this);

        mapView.onCreate(savedInstanceState);
        aMap = mapView.getMap();

        init();
    }

    /**
     * 初始化操作
     */
    private void init() {
        CameraUpdate cameraUpdate = CameraUpdateFactory.zoomBy(15.0f);
        aMap.animateCamera(cameraUpdate);

        geocodeSearch = new GeocodeSearch(this);
        geocodeSearch.setOnGeocodeSearchListener(this);

        aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
            @Override
            public void onMapLoaded() {
                geoIv.setVisibility(View.VISIBLE);
            }
        });

        aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {

            }

            @Override
            public void onCameraChangeFinish(CameraPosition cameraPosition) {
                latLng = cameraPosition.target;
                //
                LatLonPoint point = new LatLonPoint(latLng.latitude, latLng.longitude);
                RegeocodeQuery query = new RegeocodeQuery(point, 200, GeocodeSearch.AMAP);
                geocodeSearch.getFromLocationAsyn(query);
            }
        });
    }

    @OnClick(R.id.res_geo_btn)
    void resGeoBtn() {
        Toast.makeText(this, "正在地理编码", Toast.LENGTH_SHORT).show();
        GeocodeQuery geocodeQuery = new GeocodeQuery("望京soho塔一", "北京");
        geocodeSearch.getFromLocationNameAsyn(geocodeQuery);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        String address = regeocodeResult.getRegeocodeAddress().getFormatAddress();
        Toast.makeText(GeoOrResGeoActivity.this, address, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
        List<GeocodeAddress> list = geocodeResult.getGeocodeAddressList();
        Toast.makeText(this, "共得到 " + list.size() + " 条结果", Toast.LENGTH_SHORT).show();
        if (list.size() > 0) {
            LatLonPoint point = list.get(0).getLatLonPoint();
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(point.getLatitude(), point.getLongitude()), 15.0f);
            aMap.animateCamera(cameraUpdate);
            Toast.makeText(this, "latitude : " + point.getLatitude() +
                    "\n" + "longitude : " + point.getLongitude(), Toast.LENGTH_SHORT).show();
        }
    }
}
